#region Import Declarations

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.IO;

#endregion

namespace CommunityServerStuff.HttpModules
{
    /// <summary>
    /// 
    /// </summary>
    class DeletedBlogModule : IHttpModule
    {

        #region Private Members

        List<string> blogList;

        #endregion

        #region Dispose

        public void Dispose()
        {
        }

        #endregion

        #region Init

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void Init(System.Web.HttpApplication context)
        {
            context.BeginRequest += new EventHandler(CheckUrl);
            LoadBlogList();
        }

        #endregion

        #region LoadBlogList

        /// <summary>
        /// 
        /// </summary>
        /// <remarks>This function also caches the list on the server for 30 days. It uses the text file as a CacheDependency
        /// so that changes to the file automatically invalidate the cache.</remarks>
        public void LoadBlogList()
        {
            blogList = new List<string>();
            HttpContext ctx = HttpContext.Current;
            string filePath = ctx.Server.MapPath("/DeletedBlogs.txt");

            StreamReader sr = File.OpenText(filePath);
            while (sr.Peek() != -1)
            {
                blogList.Add(sr.ReadLine());
            }
            sr.Close();
            ctx.Cache.Insert("DeletedBlogs", blogList, new CacheDependency(filePath), DateTime.Now.AddDays(30), TimeSpan.Zero);
        }

        #endregion

        #region CheckUrl

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CheckUrl(object sender, EventArgs e)
        {
            HttpApplication app = ((HttpApplication)(sender));
            HttpContext ctx = HttpContext.Current;
            if (blogList == null)
            {
                if (ctx.Cache["DeletedBlogs"] == null)
                {
                    LoadBlogList();
                }
                else
                {
                    blogList = (List<string>)ctx.Cache["DeletedBlogs"];
                }

            }

            // uncomment this code to output the blogList
            //foreach (string seg in blogList)
            //{
            //    app.Response.Write(seg + "<br />");
            //}

            string[] segments = app.Request.Url.Segments;
            if (segments.Length >= 3)
            {
                if (segments[1] == "blogs/")
                {
                    if (blogList.Contains(segments[2].Replace("/", "")))
                    {
                        ctx.Response.StatusCode = 301;
                        int i = segments.Length - 1;
                        if ((segments[i].ToLower() == "rss.aspx") | (segments[i].ToLower() == "atom.aspx"))
                        {
                            //we have another file to handle feed requests so they display in an aggregator.
                            ctx.Response.Redirect("/BlogDeleted.xml", true);
                        }
                        else
                        {
                            ctx.Response.Redirect("/Msgs/default.aspx?MessageID=998", true);
                        }
                    }
                }
            }

        }

        #endregion

    }

}
